import { defineStore } from 'pinia'
import request from '@/utils/request'
import { ApiRes, CategoryItem, TopCategory } from '@/types/data'
import { topCategory } from '../constants'

const topCategoryList = topCategory.map(item => ({id: item,name: item}))

export default defineStore('category', {
  state: () => ({
    list: topCategoryList as CategoryItem[],
    topCategory: {} as TopCategory,
  }),
  actions: {
    // 获取分类列表
    async getAllCategory() {
      const res = await request.get<ApiRes<CategoryItem[]>>('/home/category/head')
      console.log(res)
      this.list = res.data.result
    },
    // 获取一级类目的信息
    async getTopCategory(id: string) {
      const res = await request.get<ApiRes<TopCategory>>('/category', {
        params: {id}
      })
      console.log('getTopCategory', res)
      this.topCategory = res.data.result
    },
  },
  persist: {
    enabled: true,
    strategies: [{key:'category'}]
  }
})